package org.dairy.farms.nandana.core.logic;

import org.dairy.farms.dao.*;
import org.dairy.farms.model.*;
import org.dairy.farms.nandana.web.view.LactationView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: GDuggira
 * Date: 21 Jun, 2012
 * Time: 5:37:13 PM
 */
@Service(value = "lactationManagementLogic")
public class LactationManagementLogicImpl implements LactationManagementLogic {

    private static final Logger logger = LoggerFactory.getLogger(LactationManagementLogicImpl.class);
    @Autowired
    private LactationDAO lactationDao;

    @Autowired
    private LivestockDAO livestockDao;

    @Autowired
    private RevenueTypeDAO revenueTypeDao;

    @Autowired
    private RevenueDAO revenueDao;
    
    @Autowired
    private LactationRevenueDAO lactationRevenueDao;

    @Autowired
    private LactationNotesDAO lactationNotesDAO;

    public Lactation getLactation(Long lactationId) {
        return lactationDao.findById(lactationId);
    }

    public List<Lactation> getLactationDetails(Long livestockId, int page, int start, int limit) {
        return lactationDao.getLactationDetails(livestockId, page, start, limit);
    }

    public Long getLactationDetailsCount(Long livestockId) {
        return lactationDao.getLactationDetailsCount(livestockId);
    }

    public List<Lactation> getAllLactationDetails(Long livestockId){
        return lactationDao.getLactationDetails(livestockId);
    }

    public Lactation updateLactation(LactationView lactationView) {
        Lactation lactation = null;
        if (lactationView.getId() == null) {
            lactation = new Lactation();
            lactation.setDate(lactationView.getDateOfLactation());
            lactation.setQuantity(lactationView.getQuantity());
            lactation.setRatePerQuantity(lactationView.getRatePerQuantity());
            lactation.setFatPercentage(lactationView.getFatPercentage());
            /*LactationRevenue lactationRevenue = createRevenue(lactationView);
            lactation.setLactationRevenue(lactationRevenue);*/
            Livestock livestock = livestockDao.findById(lactationView.getLivestockId());
            lactation.setLivestock(livestock);
            lactationDao.create(lactation);
            if (lactationView.getNotes() != null && !lactationView.getNotes().isEmpty()) {
                addLactationNotes(lactation.getId(), lactationView.getNotes());
            }
            /*lactationRevenue.setLactation(lactation);
            revenueDao.update(lactationRevenue);*/
        } else {
            lactation = lactationDao.findById(lactationView.getId());
            if (lactation != null) {
                lactation.setDate(lactationView.getDateOfLactation());
                lactation.setQuantity(lactationView.getQuantity());
                lactation.setRatePerQuantity(lactationView.getRatePerQuantity());
                lactation.setFatPercentage(lactationView.getFatPercentage());
                //lactation.setRevenue((lactationView.getQuantity() * lactationView.getRatePerQuantity()));
                LactationRevenue lactationRevenue = updateRevenue(lactation);
                //lactation.setLactationRevenue(lactationRevenue);
                lactationDao.update(lactation);
            }
        }
        return lactation;
    }

    private LactationRevenue createRevenue(LactationView lactationView) {
        return createRevenue((float) lactationView.getQuantity(), lactationView.getRatePerQuantity(), lactationView.getDateOfPayment());
    }

    private LactationRevenue createRevenue(float quantity, double ratePerQuantity, Date dateOfPayment) {
        LactationRevenue lactationRevenue = new LactationRevenue();
        lactationRevenue.setAmount((float) (quantity * ratePerQuantity));
        lactationRevenue.setPaymentDate(dateOfPayment);
        RevenueType revenueType = revenueTypeDao.findByName(RevenueType.LACTATION_REVENUE);
        lactationRevenue.setRevenueType(revenueType);
        revenueDao.create(lactationRevenue);
        return lactationRevenue;
    }

    private LactationRevenue updateRevenue(Lactation lactation) {

        LactationRevenue lactationRevenue = null;
       /* if (lactation.getLactationRevenue() == null) {

            logger.debug("Unable to find lactation revenue record... data error so trying to create a new one.");
            lactationRevenue = createRevenue(lactation.getQuantity(), lactation.getRatePerQuantity(), lactation.getDate());
        } else {
            lactationRevenue = lactationRevenueDao.findById(lactation.getLactationRevenue().getId());
        }
        lactationRevenue.setAmount((float) (lactation.getQuantity() * lactation.getRatePerQuantity()));
        RevenueType revenueType = revenueTypeDao.findByName(RevenueType.LACTATION_REVENUE);
        lactationRevenue.setRevenueType(revenueType);
        lactationRevenue.setLactation(lactation);
        revenueDao.update(lactationRevenue);*/
        return lactationRevenue;
    }

    @Override
    public Notes addLactationNotes(Long lactionId, String notes) {
        LactationNotes lactationNotes = new LactationNotes();
        lactationNotes.setNote(notes);
        lactationNotes.setLactation(lactationDao.findById(lactionId));
       return lactationNotesDAO.create(lactationNotes);
    }
}
